<template>
  <!-- 工作票审核 -->
  <a-modal
    v-model:open="visible"
    title="工作票审核"
    @ok="handleOk"
    class="addStation"
    destroyOnClose
    :width="480"
    @cancel="cancel"
    centered
  >
    <a-form
      :model="form"
      :rules="rules"
      layout="inline"
      :labelCol="labelCol"
      ref="formRef"
    >
      <a-form-item label="审核结果" name="passFlag">
        <a-radio-group
          v-model:value="form.passFlag"
          size="default"
          button-style="solid"
        >
          <a-radio :value="1">通过</a-radio>
          <a-radio :value="0">驳回</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="处理描述" v-if="form.passFlag === 1">
        <a-textarea
          v-model:value="form.approveDesc"
          class="formInput"
          placeholder="请输入"
          type="textarea"
          allow-clear
          :rows="3"
        />
      </a-form-item>
      <a-form-item
        label="驳回原因"
        name="approveDesc"
        v-if="form.passFlag === 0"
      >
        <a-textarea
          v-model:value="form.approveDesc"
          class="formInput"
          placeholder="请输入"
          type="textarea"
          allow-clear
          :rows="3"
        />
      </a-form-item>
      <a-form-item label="上传附件">
        <a-upload
          v-model:file-list="fileList"
          name="file"
          list-type="picture-card"
          class="addEquipUpload"
          :action="uploadUrl"
          :headers="headers"
          method="post"
          :multiple="true"
          :preview-file="previewFile"
          :before-upload="beforeUpload"
          @change="handleUploadChange"
          @remove="handleRemove"
        >
          <div>
            <loading-outlined v-if="loading"></loading-outlined>
            <plus-outlined v-else></plus-outlined>
            <div class="ant-upload-text">上传</div>
          </div>
        </a-upload>
      </a-form-item>
    </a-form>
    <template #footer>
      <div class="footerBar">
        <a-button key="back" @click="cancel">取消</a-button>
        <a-button
          key="submit"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确认</a-button
        >
      </div>
    </template>
  </a-modal>
</template>
<script setup>

import storage from "@/utils/storage"
import { WORK_ORDER_TYPE } from "@/constants/workOrder"
const $api = inject("$api")
const props = defineProps({
  ticketAuditVisible: {
    type: Boolean,
    default: false,
  },
  rowData: {
    type: Object,
  },
  stationtList: {
    type: Array,
  },
})
const visible = ref(props.ticketAuditVisible)
const emits = defineEmits(["update:ticketAuditVisible", "getOrderList"])
const fileList = ref([])
const userInf = storage.getItem("USER_INFO")
const headers = ref({
  Authorization: "Bearer " + userInf.access_token,
})
watch(
  () => props.ticketAuditVisible,
  async (val) => {
    visible.value = val
  }
)
const labelCol = { style: { width: "100px" } }
const formRef = ref(null)
const form = ref({
  passFlag: 1,
  approveDesc: undefined,
  approveAttach: undefined,
})
const rules = ref({
  result: [
    {
      required: true,
      message: "请选择",
      trigger: "change",
    },
  ],
  approveDesc: [
    {
      required: true,
      message: "请输入",
      trigger: "blur",
    },
  ],
})
const cancel = () => {
  emits("update:ticketAuditVisible", false)
  formRef.value.clearValidate()
  form.value = {
    passFlag: 1,
    approveDesc: undefined,
    approveAttach: undefined,
  }
  fileList.value = []
}
const loading = ref(false)
const handleSubmit = () => {
  formRef.value
    .validate()
    .then(async () => {
      const params = {
        ...form.value,
        id: props.rowData.id,
        type: WORK_ORDER_TYPE.TICKET,
        approveAttach: fileList.value
          .map((item) => item.response?.data?.id)
          .join(","),
      }
      loading.value = true
      const res = await $api.ticketApprove(params)
      loading.value = false
      if (res && res.code === 0) {
        message.success("审核成功")
        cancel()
        emits("getOrderList")
      }
    })
    .catch((error) => {
      console.log("error", error)
    })
}
const uploadUrl = async (val) => {
  return `${$api.uploadAction().url}`
}
const handleUploadChange = (data) => {
  if (data.file?.response?.code === 0) {
    // form.value.approveAttach = data.file.response.data.id.toString();
  }
}
</script>

<style lang="less" scoped>
.form {
  padding: 0 40px;
  margin-top: 30px;
}
.formInput,
.formSelect {
  width: 260px;
}
.ant-form-item {
  margin-top: 20px;
}
.footerBar {
  text-align: center;
  margin-top: 30px;
}
.addEquipUpload {
  .anticon-eye {
    display: none;
  }
}
.ant-upload-list {
  width: 340px;
}
</style>
